package com.tanhua.server.service;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.tanhua.server.mapper.UserMapper;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;


@Service
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserMapper userMapper;

    @Value("${tanhua.sso.url}")
    private String url;

    @Autowired
    private ObjectMapper MAPPER = new ObjectMapper();

    public User queryUserByToken(String token) {
        String userStr = restTemplate.getForObject(url + "/user/{token}" , String.class,token);
        if (StringUtils.isNotEmpty(userStr)){
            try {
                return MAPPER.readValue(userStr, User.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public User queryUserById(Long id) {
        return userMapper.selectById(id);
    }
}
